package com.rybbaby.tss.tests.validator.controller;

import java.util.ArrayList;
import java.util.List;

import com.rybbaby.tss.core.annotation.validator.BetweenValidator;
import com.rybbaby.tss.core.annotation.validator.CreditCardValidator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.rybbaby.tss.core.annotation.RequestLog;
import com.rybbaby.tss.core.annotation.validator.Base64Validator;
import com.rybbaby.tss.core.annotation.validator.CustomValidator;
import com.rybbaby.tss.core.annotation.validator.DateValidator;
import com.rybbaby.tss.core.annotation.validator.EmailAddressValidator;
import com.rybbaby.tss.core.annotation.validator.IPValidator;
import com.rybbaby.tss.core.annotation.validator.PhoneValidator;
import com.rybbaby.tss.core.annotation.validator.StringValidator;
import com.rybbaby.tss.core.annotation.validator.StringValidator.StringCaseType;
import com.rybbaby.tss.core.verifier.AbstractVerifier;

/**
 * @description 测试验证注解作用户方法传入参数
 * 
 * @author 谢进伟
 * 
 * @createDate 2016年4月1日 下午6:40:54
 */
@Controller
@RequestMapping(value = "methodParamsAutoValidateTest")
public class MethodParamsAutoValidateTestController {
	
	@RequestLog(content = "方法参数验证器测试")
	@ResponseBody
	@RequestMapping(value = "test1")
	public String test1(@Base64Validator(nullable = false) String name , @BetweenValidator(min = 10 , max = 120 , includeMin = false) Integer age , @CreditCardValidator(nullable = false) String creditCard , @DateValidator(nullable = false) String date , @EmailAddressValidator(nullable = false) String email , @IPValidator String [] ip , @PhoneValidator String phone , @StringValidator(caseType = StringCaseType.UPPERCASEORLOWERCASE , length = 3 , pureChinese = true) String remark , @CustomValidator(verifier = UserNameVerfier.class) String userName) {
		return "测试成功!";
	}
	
	@RequestMapping("test2")
	public ModelAndView test2(@BetweenValidator(min = 0 , max = 10) Integer count) {
		ModelAndView mav = new ModelAndView("index.jsp");
		return mav;
	}
	
	@RequestMapping("test3")
	public String test3(@BetweenValidator(min = 0 , max = 10) Integer count) {
		return "index.jsp";
	}
	
	@RequestMapping("test4")
	public String test4(ValidatorTempClass vtc) {
		return "index.jsp";
	}
	
	@RequestMapping("test5")
	public String test5(ValidatorTempClass vtc , @BetweenValidator(min = 2010 , max = 2016) Integer year) {
		return "index.jsp";
	}
	
	/**
	 * @description 自定义验证用户名的验证器<br>
	 * 
	 *              本列演示验证userName不能是已存在的用
	 * 
	 * @author 谢进伟
	 * 
	 * @createDate 2016年4月1日 上午11:44:42
	 */
	public static class UserNameVerfier extends AbstractVerifier {
		
		private static List<String> list = new ArrayList<String>();
		static {
			list.add("admin");
			list.add("xiejinwei");
		}
		
		@Override
		public boolean validate(Object params) {
			boolean result = validate(params , false , null , new ValidationRule() {
				
				@Override
				public boolean check(Object params) {
					return !list.contains(params);
				}
			});
			if(!result && params != null) {
				this.errorMsg = params + "已存在";
			}
			return result;
		}
	}
}
